import os
import re
import pickle
import sys
#from terminaltables import AsciiTable

'''
istart == 0  icharg < 10  nsw == 0
'''

def getparas(Outcar_path):
    
    ISTART = os.popen("grep -a ISTART "+ Outcar_path +" |awk '{print $3}'").read()
    #print type(ISTART)
    ICHARG = os.popen("grep -a ICHARG "+ Outcar_path +" |awk '{print $3}'").read()
    #print type(ICHARG)
    NSW = os.popen("grep -a NSW "+ Outcar_path +" |awk '{print $3}'").read()
    #print type(NSW)
    '''
    #TAG_MEAR =str(0)
    if (ISTART==0 and ICHARG<10 and NSW==0): PASS = str(1)
    else: PASS = str(0)
    '''
    #ENCUT = str(os.popen("grep 'ENCUT' "+ Outcar_path +" |awk '{print $3}'").read()).strip()
    ENCUT = str(os.popen("grep -a -n 'ENCUT' "+ Outcar_path +" |grep -n ''|grep '^1:'|awk -F':' '{print $3}'|awk '{print $3}'").read()).strip()
    ismear = str(os.popen("grep -a ISMEAR "+ Outcar_path +" |awk '{print $3}'").read())
    print ("ismear:"+ismear)
    ISMEAR = ismear[:ismear.find(';')].strip()
    print ("ISMEAR:"+ISMEAR)
    #SIGMA = str(os.popen("grep SIGMA "+ Outcar_path +" |awk '{print $6}'").read()).strip()
    SIGMA = str(os.popen("grep -a -n 'SIGMA' "+ Outcar_path +" |grep -n ''|grep '^1:'|awk -F':' '{print $3}'|awk '{print $6}'").read()).strip()
    NELECT = str(os.popen("grep -a NELECT "+ Outcar_path +" |awk '{print $3}'").read()).strip()
    ISPIN = str(os.popen("grep -a ISPIN "+ Outcar_path +" |awk '{print $3}'").read()).strip()
    EDIFF = str(os.popen("grep -a 'stopping-criterion for ELM' "+ Outcar_path +" |awk '{print $3}'").read()).strip()
    NELM = str(os.popen("grep -a NELM "+ Outcar_path +" |awk '{print $3}'").read()).strip()
    #print EDIFF

    ISTART = str(ISTART).strip()
    ICHARG = str(ICHARG).strip()
    NSW = str(NSW).strip()

    if (ISTART and ICHARG and NSW):
        if (int(ISTART)==0 and int(ICHARG)<10 and int(NSW)==0): PASS = str(1)
        else: PASS = str(0)
    else: PASS = 'null'
    #print type(ISMEAR)
    #if ISMEAR==0 : TAG_MEAR = 1
    '''
    paraslist = []`
    for para in [PASS,ISTART,ICHARG,NSW,ENCUT,ISMEAR,SIGMA,NELECT,ISPIN,EDIFF]:
        digit_para = re.findall(r"-?\d+\.?\d+(E-)?\d*",para)
	print digit_para
        paraslist.append(str(digit_para))
    '''

    paras_list = [PASS,ISTART,ICHARG,NSW,ENCUT,ISMEAR,SIGMA,NELECT,ISPIN,EDIFF,NELM]
    paras_dict = {'PASS': PASS,'ISTART': ISTART,'ICHAR': ICHARG,'NSW': NSW,'ENCUT': ENCUT,'ISMEAR': ISMEAR,'SIGMA': SIGMA,'NELECT': NELECT,'ISPIN': ISPIN,'EDIFF': EDIFF,'NELM': NELM}
    
    return paras_list, paras_dict



def getdict(paras_dict,id_dir,paras_file):
    id_dict = {id_dir: paras_dict}
    
    
def getcsv(paras_list,id_dir,paras_file):
    idfile = open(paras_file,'a')
    idfile.write(id_dir)
    for p in paras:
        idfile.write(',')
        idfile.write(p)
        #idfile.write('\000')
        idfile.write("\n")
    idfile.close()


def scan (extractpath,paras_file,file_type):
    id_dict = {}
    for id_dir in os.listdir(extractpath):
        outcar_path = extractpath + '/' + id_dir + '/OUTCAR'
        if os.path.exists(outcar_path):
            print id_dir
            paras = getparas(outcar_path)
            id_dict[id_dir] = paras[1]
        else: continue
    output = open(paras_file, 'wb')
    pickle.dump(id_dict, output)
    output.close()



if __name__ == '__main__':
    #ExtractPath = "/home/lss/201711_extract_data"
    ExtractPath = sys.argv[1]
    #------- .pkl -------------
    csv_file = '201711_paras.csv'
    #pkl_file = '201711_dict_paras.pkl'
    pkl_file = sys.argv[2]
    #------ list or dict ------
    file_type = 'dict'  
    scan(ExtractPath,pkl_file,file_type)



